package cat.joronya.supermanager.market;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import cat.joronya.supermanager.R;

public class BuyDialogFragment extends DialogFragment
{
	// pel Log
	private static final String TAG = "supermanager";
	
	public static BuyDialogFragment newInstance(String idequ, String idjug, String name, boolean selectable)
	{
		BuyDialogFragment frag = new BuyDialogFragment();
		Bundle args = new Bundle();
		args.putString("cat.joronya.supermanager.martket.buy.idequ", idequ);
        args.putString("cat.joronya.supermanager.martket.buy.idjug", idjug);
        args.putString("cat.joronya.supermanager.martket.buy.name", name);
        args.putBoolean("cat.joronya.supermanager.martket.buy.selectable", selectable);
        frag.setArguments(args);
        return frag;
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) 
	{
		final String idequ = getArguments().getString("cat.joronya.supermanager.martket.buy.idequ");
		final String idjug = getArguments().getString("cat.joronya.supermanager.martket.buy.idjug");
		final String name = getArguments().getString("cat.joronya.supermanager.martket.buy.name");
		final boolean selectable = getArguments().getBoolean("cat.joronya.supermanager.martket.buy.selectable");

		AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
		alertDialog.setTitle(getString(R.string.market_dialog_buy_title));
		alertDialog.setMessage(getString(R.string.market_dialog_buy_message, name));
		
		alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, 
				 getString(R.string.market_dialog_buy_ok),
				 new DialogInterface.OnClickListener()
		 {
			 public void onClick(DialogInterface dialog, int which) 
			 {
				 Log.d(TAG,"MarketActivityList ok picking player["+name+"] to buy!");
				 if( selectable )
				 {
					 // build result and set it
					 Log.d(TAG, "MarketActivityList player["+idjug+"] selected!");
					 
					 Intent data = new Intent();
					 data.putExtra("cat.joronya.supermanager.myteams.idequ", Long.parseLong(idequ));
					 data.putExtra("cat.joronya.supermanager.market.buyed_player", idjug);
					 getActivity().setResult(Activity.RESULT_OK, data);
					 getActivity().finish();
				 }
				 else
				 {
					 Log.d(TAG, "MarketActivityList player not selectable!");
				 }
				 return;
			 } 
		 }); 
		 alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
				 getString(R.string.market_dialog_buy_cancel),
				 new DialogInterface.OnClickListener()
		 {
			 public void onClick(DialogInterface dialog, int which) 
			 {
				 Log.d(TAG,"MarketActivityList cancel picking player["+name+"]!");
				 return;
			 } 
		 }); 
		 
		return alertDialog;
	}
}
